import util from '../../../utils/util.js'
import api from '../../../config/api.js'

const formatTime = util.formatTime;
const app = getApp();
Page({
  data: {
    waypoint: null,
    replies: {
      recommender_count: 0,
      recommenders: [],
      comments: [],
      comment_count: 0,
    },
    title: '',
  },
  onReady() {
    const self = this;
    wx.setNavigationBarTitle({
      title: self.data.title,
    });
  },
  onLoad(options) {
    const self = this;
    const tripId = options.tripId;
    const waypointId = options.waypointId;
    this.getWaypointDetail(tripId, waypointId);
  },
  getWaypointDetail(tripId, waypointId) {
    const self = this;
    util.request(api.waypointInfoByID + tripId + '/waypoints/' + waypointId + '/').then(function (res) {
      const waypoint = res;
      self.setData({
        title: waypoint.trip_name,
        waypoint,
      });
      if (waypoint.comments > 0) {
        self.getWaypointReplies(tripId, waypointId);
      }
    });
  },
  getWaypointReplies(tripId, waypointId) {
    const self = this;
    util.request(api.waypointReplyByID + tripId + '/waypoints/' + waypointId + '/replies/').then(function (res) {
      const replies = res;
      replies.comments.map((reply) => {
        const item = reply;
        item.date_added = formatTime(new Date(item.date_added * 1000), 2);
        return item;
      });
      self.setData({
        replies,
      });
    });
  },
  gotoUser(e) {
    const userId = e.target.dataset.id;
    wx.navigateTo({
      url: `../user/user?id=${userId}`,
    });
  },
  previewImage(e) {
    const url = e.currentTarget.dataset.src;
    wx.previewImage({
      current: url,
      urls: [url],
    });
  },
});
